package service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import dao.DishAuditDao;
import po.DishAudit;
import service.DishAuditManager;
import service.WebService;
import vo.DishAuditVo;
import vo.Dish;

@Service("dishAuditManager")
public class DishAuditManagerImpl implements DishAuditManager {

	@Autowired
	private DishAuditDao dad;
	@Autowired
	private WebService ws;
	
	@Override
	public List<DishAuditVo> listAllDishAudit() {
		// TODO Auto-generated method stub
		List<String> didList = new ArrayList<String>();
		List<DishAudit> list = dad.listAllDishAudit();
		List<DishAuditVo> dishList = new ArrayList<DishAuditVo>();
		for (DishAudit dishAudit : list) {
			didList.add(dishAudit.getdId());
		}
		Map<String, Dish> dishMap = ws.listDishByDId(didList);
		for (DishAudit dishAudit_: list) {
			dishList.add(new DishAuditVo(dishMap.get(dishAudit_.getdId()), 
					dishAudit_.getExpiredDate(), dishAudit_.getStatus(), 
					dishAudit_.getCreateDate()));
		}
		return dishList;
	}

	@Override
	public DishAudit findDishAuditById(String id) {
		// TODO Auto-generated method stub
		return dad.findDishAuditById(id);
	}

	@Override
	public List<String> findRecommendDish() {
		// TODO Auto-generated method stub
		return dad.findRecommendDish();
	}

	@Override
	public String addDishAudit(DishAudit dishAudit) {
		// TODO Auto-generated method stub
		return dad.insertDishAudit(dishAudit);
	}

	@Override
	public String auditDish(String dId, int status) {
		// TODO Auto-generated method stub
		DishAudit dishAudit = dad.findDishAuditById(dId);
		if(dishAudit != null){			
			dishAudit.setStatus(status);
			Date date = new Date();
			dishAudit.setExpiredDate(new Date(date.getTime() + 7*24*60*60*1000));
			return dad.updateDishAudit(dishAudit);
		}else {
			return null;
		}
	}

	@Override
	public Map<String, Integer> listDishAuditByMId(String mId) {
		// TODO Auto-generated method stub
		List<DishAudit> list = dad.listDishAuditByMId(mId);
		Map<String, Integer> dishList = new HashMap<String, Integer>();
		for (DishAudit dishAudit : list) {
			dishList.put(dishAudit.getdId(), dishAudit.getStatus());
		}
		return dishList;
	}

}
